Skip to content

Improve dispatch to a real scheduler using preemption#117

Merged
LuernOutOfOrder merged 44 commits intodevelopfrom
106-preemptif-scheduler-trap-frame-reset
Feb 19, 2026
Merged

Improve dispatch to a real scheduler using preemption#117
LuernOutOfOrder merged 44 commits intodevelopfrom
106-preemptif-scheduler-trap-frame-reset

Conversation

@LuernOutOfOrder
Copy link
Owner

This pull request introduces significant improvements to the kernel's scheduler and context switching mechanisms, adds new documentation, and refactors several core data structures. The most notable changes include a complete rewrite of the scheduler logic (including the introduction of run and blocked queues), the addition of a bitmap utility for efficient priority management, and a major refactor of context save/restore assembly routines. Documentation has also been expanded to describe new and existing data structures and scheduler behavior.

Kernel Scheduler and Task Queues:

  • Added a comprehensive new documentation file describing the kernel scheduler, including its run and blocked queues, preemption, scheduling model, and invariants. The run queue now uses a bitmap for fast priority lookup, and the blocked queue is implemented as a sorted indexed linked list.
  • Introduced new configuration constants for task priorities and queue sizes in src/config.rs, and updated documentation to reflect these changes. [1] [2]

Data Structures and Utilities:

  • Added a new Bitmap struct in src/primitives/bitmap.rs to efficiently manage and query task priorities in the run queue.
  • Documented the new IndexedLinkedList data structure and its invariants in Documentation/kernel/data_structure.md. [1] [2]

Context Switching and Trap Handling:

  • Refactored RISC-V context switch assembly routines: removed saving/restoring of mstatus in the task context, rewrote save_context/restore_context to handle privilege and interrupt bits directly, and introduced trap_save_context/trap_restore_context for trap entry/exit. [1] [2] [3] [4] [5] [6] [7] [8]
  • Updated the TaskContext struct in Rust to remove the mstatus field and adjust offsets accordingly. [1] [2]

Miscellaneous and Documentation:

  • Increased RAM and ROM sizes in the linker script for test mode.
  • Updated kernel version to 0.4.5 in both Cargo.toml and src/info.rs. [1] [2]
  • Enabled the stmt_expr_attributes nightly feature in src/main.rs.

These changes collectively modernize the kernel's scheduling and context switching logic, improve documentation, and lay groundwork for further extensibility and debugging.

…future use

Also update the type of task max priority in config file
…e, add new task public API to get task's priority
…tive, add a new delta_list primitive and update in scheduler
…ocked queue, update task primitive as well

The task primitive now use correctly the new blocked queue, they don't
touch the queues directly, just trigger a reschedule if needed.
The scheduler will check the blocked queue and awake the head if a
need_reschedule has been trigger.
…tly update the current_task, also improve the need_resched flag use in scheduler
Preemptive scheduler working, there's an exception after few cycles,
don't know why
…me keep running

Still the low priority task restarting from 0.
…pdate trap entry to save the task context and restore it if there's no need reschedule

Remove the mstatus save, risky in trap handling if mstatus change
…, fix task context to use correct mstatus value

Task context mstatus has been update to use 6152 instead of 8 to enable
M-mode on task,for now at least
Things are smelly, like if two task with different priority print, it
can cause race condition and corrupt uart driver leading to a complete
kernel crash(mcause = 7), but if the print or logging system is
protected with a mutex, or while printing, we deactivate interrupt,
there's no problem.
@LuernOutOfOrder LuernOutOfOrder added this to the v0.4.5 milestone Feb 19, 2026
@LuernOutOfOrder LuernOutOfOrder self-assigned this Feb 19, 2026
@LuernOutOfOrder LuernOutOfOrder added documentation Improvements or additions to documentation enhancement New feature or request labels Feb 19, 2026
@LuernOutOfOrder LuernOutOfOrder merged commit 88338a0 into develop Feb 19, 2026
3 checks passed
@LuernOutOfOrder LuernOutOfOrder deleted the 106-preemptif-scheduler-trap-frame-reset branch February 19, 2026 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant